package db

import (
	"fmt"
	"os"
	"time"

	"github.com/go-redis/redis"
)

type RedisClient struct {
	*redis.Client
}

var (
	MainRedisClient *redis.Client
	NOW_DATE        = ""
)

const (
	TIME_FORMAT = "2006-01-02"
)

func InitRedis() error {
	now := time.Now()
	NOW_DATE = now.Format(TIME_FORMAT)
	MainRedisClient = redis.NewClient(&redis.Options{
		Addr:     os.Getenv("REDIS_HOST"),
		Password: os.Getenv("REDIS_HOST_PASSWORD"), // no password set
		DB:       2,                                // use default DB
	})

	_, err := MainRedisClient.Ping().Result()
	return err
}

func SetLoginStats(uid string) error {
	now := time.Now()
	now_date := now.Format(TIME_FORMAT)
	if now_date != NOW_DATE {
		NOW_DATE = now_date
	}
	key := fmt.Sprintf("DAU:SET:%s", NOW_DATE)
	return MainRedisClient.PFAdd(key, uid).Err()
}
